package com.cgbl.service.ui

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ColorUtils
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.SelectProjectListAdapter
import com.cgbl.service.databinding.AppActivitySelectProjectBinding
import com.cgbl.service.view.CGTimePickerBuilder
import com.cgbl.service.vm.SelectProjectViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.ProjectBean
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import java.util.*

@Route(path = AppConstants.Router.Project.A_SELECT_PROJECT)
class SelectProjectActivity :
    GLBaseActivity<AppActivitySelectProjectBinding, SelectProjectViewModel>() {

    lateinit var selectProjectListAdapter: SelectProjectListAdapter


    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_select_project

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        selectProjectListAdapter = SelectProjectListAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = selectProjectListAdapter
        }
        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.refresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
            }
        })
        viewModel.refresh()
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        val rightView = findViewById<TextView>(R.id.tv_right_text)
        titleView?.text = "所属项目"
        backView?.setOnClickListener { finish() }
        rightView.visibility = View.VISIBLE
        rightView.text = "确定"
        rightView.setTextColor(ColorUtils.getColor(R.color.app_color_cc2d2d))
    }

    override fun initViewObservable() {
        viewModel.uc.refreshProjectDataEvent.observe(this) {
            selectProjectListAdapter.setNewInstance(it as MutableList<ProjectBean>?)
            finishLoad()
        }
        viewModel.uc.loadMoreProjectDataEvent.observe(this) {
            selectProjectListAdapter.addData(it)
            finishLoad()
        }
    }

    private fun finishLoad() {
        binding.refreshLayout.finishLoadMore()
        binding.refreshLayout.finishRefresh()
    }
}